﻿using System;

namespace jisuanqi
{
    class Program
    {
        static void Main(string[] args)
        {
            double n1 = 0;//计算数1
            double n2 = 0;//计算书2
            bool f = true;//判断条件
            bool isSD = true;//是否为双目运算
            while (f)
            {
                Console.WriteLine("输入第一个数");
                IsNumber(out n1);//输入并判断第一个数是否满足条件


                Console.WriteLine("输入运算符 + - * / ^(a的b次方) sqrt(平方根) sin(正弦) cos(余弦)");
                string str = Iso(out isSD);//检擦运算符
                if (isSD)
                {
                    //当为双目运算时
                    Console.WriteLine("输入第二个数");
                    IsNumber(out n2);//输入并判断第二个数是否满足条件
                    Console.WriteLine("计算结果为：{0}", JiSuan(str, ref n1, ref n2));//计算
                }
                else
                {
                    //当为单目运算时
                    Console.WriteLine("计算结果为：{0}", JiSuan(str, ref n1));//输出结果
                }

                Console.WriteLine("退出请按“Q”，继续请按其他任意键");
                ConsoleKeyInfo s = Console.ReadKey();
                if (s.Key == ConsoleKey.Q)
                {
                    //当输入为Q时 退出程序
                    f = false;
                }
                else
                {
                    //当不为Q时 继续下一轮计算，并清屏
                    f = true;
                    Console.Clear();
                }
            }

            Console.ReadKey();
        }

        /// <summary>
        /// 检擦输入能否转化为数值
        /// </summary>
        /// <param name="d">转化后的结果</param>
        public static void IsNumber(out double d)
        {
            string s;
            bool f = true;
            //当转化成功时 将转化后的结果赋给变量 d，并将f置为false 跳出循环
            //转化失败时，继续循环输入
            do
            {
                s = Console.ReadLine();

                bool isOK = double.TryParse(s, out d);
                if (isOK)
                {
                    d = Convert.ToDouble(s);
                    f = false;
                    return;
                }
                Console.WriteLine("输入错请重新输入");
            } while (f);
        }

        /// <summary>
        /// 双目计算
        /// </summary>
        /// <param name="str">运算符</param>
        /// <param name="a1">运算数1</param>
        /// <param name="a2">运算数2</param>
        /// <returns>double</returns>
        public static double JiSuan(string str, ref double a1, ref double a2)
        {
            double result = 0;
            switch (str)
            {
                case "+":
                    result = a1 + a2;
                    break;
                case "-":
                    result = a1 - a2;
                    break;
                case "*":
                    result = a1 * a2;
                    break;
                case "/":
                    result = a1 / a2;
                    break;
                case "^":
                    result = Math.Pow(a1, a2);
                    break;
            }
            return result;
        }

        /// <summary>
        /// 单目计算
        /// </summary>
        /// <param name="str">运算符</param>
        /// <param name="a1">运算数</param>
        /// <returns></returns>
        public static double JiSuan(string str, ref double a1)
        {
            double result = 0;
            switch (str)
            {
                case "sqrt":
                    result = Math.Sqrt(a1);
                    break;
                case "sin":
                    result = Math.Sin(Math.PI / (180 / a1));
                    break;
                case "cos":
                    result = Math.Cos(Math.PI / (180 / a1));
                    break;
            }
            return result;
        }

        /// <summary>
        /// 检查是单目运算还是双目运算
        /// </summary>
        /// <param name="ds">当为双目运算符时 ds设为true，否则为false</param>
        /// <returns>返回运算符</returns>
        public static string Iso(out bool ds)
        {
            string str = Console.ReadLine();
            string o = "+ - * / ^(a的b次方) sqrt(平方根) sin(正弦) cos(余弦)";
            string s = " + - * / ^ ";//双目运算符

            //检查运算符输入是否正确，如果不正确重新输入
            bool f = true;
            while (f)
            {
                //输入的字符是否包含在运算符串o中
                if (o.Contains(str))
                {
                    f = false;
                }
                else
                {
                    Console.WriteLine("运算符输入有误,请重新输入");
                    str = Console.ReadLine();
                }
            }

            //判断是否为双目运算
            if (s.Contains(str))
            {
                ds = true;
            }
            else
            {
                ds = false;
            }


            return str;
        }
    }
}
